package com.loong.musicdemo.ui.find.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.tabs.TabLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;


import com.loong.musicdemo.R;
import com.loong.musicdemo.model.Singer;
import com.loong.musicdemo.ui.find.activity.ui.detail.SectionsPagerAdapter;
import com.loong.musicdemo.util.BlurUtil;
import com.loong.musicdemo.util.dataDeal.SingerDeal;

import de.hdodenhof.circleimageview.CircleImageView;

public class ShowSingerDetailActivity extends AppCompatActivity {
    private CollapsingToolbarLayout bcImg;
    private String accountId="";
    private String bg = "";
    private CircleImageView singer_image;
    private TextView singer_name,follow_info;
    private SectionsPagerAdapter sectionsPagerAdapter;
    private ViewPager viewPager;
    private TabLayout tabs;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        setContentView(R.layout.activity_show_singer_detail);
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);//若工具栏可用,则显示Home菜单
        }
        initView();
        getSingerIntent();
        Glide.with(this).asBitmap()
                .load(bg)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        Bitmap bitmap = BlurUtil.doBlur(resource, 1, 1);
                        bcImg.setBackground(new BitmapDrawable(bitmap));
                    }
                });

        new SingerTask().execute();

    }
    private void initView(){
        viewPager = findViewById(R.id.view_pager);
        tabs = findViewById(R.id.tabs);
        bcImg = findViewById(R.id.toolbar_layout);
        singer_image = findViewById(R.id.singer_image);
        singer_name = findViewById(R.id.singer_name);
        follow_info = findViewById(R.id.follow_info);
    }

    private void getSingerIntent(){
        Intent intent = getIntent();
        bg = intent.getStringExtra("singerBg");
        accountId = intent.getStringExtra("singerAccountId");
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;//返回前一个活动
        }
        return super.onOptionsItemSelected(item);
    }

    class SingerTask extends AsyncTask<String,Void, Singer>{
        @Override
        protected void onPostExecute(Singer singer) {
            super.onPostExecute(singer);
            sectionsPagerAdapter = new SectionsPagerAdapter(getApplicationContext(), getSupportFragmentManager(),singer);
            viewPager.setAdapter(sectionsPagerAdapter);
            tabs.setupWithViewPager(viewPager);
            Glide.with(getApplicationContext()).load(singer.getImg1v1Url())
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .into(singer_image);
            singer_name.setText(singer.getName());
            follow_info.setText("关注:"+singer.getFollows()+" "+"粉丝:"+ singer.getFolloweds());
        }

        @Override
        protected Singer doInBackground(String... strings) {
            return SingerDeal.getSingerById(accountId);
        }
    }
}